import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import filter  from "./filter/filter";

axios.defaults.baseURL = process.env.VUE_APP_SERVER;
//解决每次axios请求，对应的sessionid不一样的问题
axios.defaults.withCredentials=true;

/**
 * axios拦截器
 */
axios.interceptors.request.use(function (config) {
    console.log('请求参数：', config);
    const token =Tool.getLoginUser().token;
    if (Tool.isNotEmpty(token)) {
        config.headers.token = token;
        console.log("请求headers增加token:", token);
    }
    return config;
}, error => {
    return Promise.reject(error);
});
axios.interceptors.response.use(function (response) {
    console.log('返回结果：', response);
    return response;
}, error => {
    console.log('返回错误：', error);
    const response = error.response;
    const status = response.status;
    if (status === 401) {
        // 判断状态码是401 跳转到首页或登录页
        console.log("未登录，跳到首页");
        Tool.setLoginUser(null);
        Toast.error("未登录|登录超时|无权限");
        router.push('/');
    }
    return Promise.reject(error);
});

// 路由登录拦截
router.beforeEach((to, from, next) => {
    // 要不要对meta.loginRequire属性做监控拦截
    if (to.matched.some(function (item) {
        console.log(item, "是否需要登录校验：", item.meta.loginRequire);
        return item.meta.loginRequire
    })) {
        const loginUser = Tool.getLoginUser();
        if (Tool.isEmpty(loginUser)) {
            console.log("用户未登录！");
            next('/');
        } else {
            next();
        }
    } else {
        next();
    }
});

const app=createApp(App);
app.use(store).use(router).mount('#app')




//全局过滤器
app.config.globalProperties.$filters = {
    optionKV(object, key){
        if (!object || !key) {
            return "";
        } else {
            let result = "";
            for(let enums in object){
                if (key === object[enums]["key"]) {
                    result = object[enums]["value"];
                }
            }
            return result;
        }
    },
    PrefixInteger(num, length) {
    return (Array(length).join('0') + num).slice(-length);
    },
     formatSecond(value){
        value = value || 0;
        let second = parseInt(value, 10); // 秒
        let minute = 0; // 分
        let hour = 0; // 小时
        if (second > 60) {
            // 当大于60秒时，才需要做转换
            minute = Math.floor(second / 60);
            second = Math.floor(second % 60);
            if (minute > 60) {
                hour = Math.floor(minute / 60);
                minute = Math.floor(minute % 60);
            }
        } else {
            // 小于60秒时，直接显示，不需要处理
        }
        let result = "" + this.PrefixInteger(second, 2) + "";
        // 拼上分钟
        result = "" + this.PrefixInteger(minute, 2) + ":" + result;
        // 拼上小时
        result = "" + this.PrefixInteger(hour, 2) + ":" + result;
        return result;
    },
     formatFileSize(value){
        value = value || 0;
        let result;
        //文件大于100kb
        if (value > 100 * 1024) {
            result = Math.round((value / 1024 / 1024) * 100) / 100 + "MB";
        } else {
            result = Math.round((value / 1024) * 100) / 100 + "KB";
        }
        return result;
    }
};